<template>
  <!--批量活动执行情况查询 -经销商端-->
  <div>
    <TopFilter ref="topfilter" :list="topFilterList" :cols="4" :collapse="true" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :isExportExcel="false"
      exportFileName="批量活动执行情况查询.xlsx"
      :onCalcExportData="handleExportFun"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button type="primary" :click="exportHandle.bind(this)">导出</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="visible" title="参与车辆明细" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" destroyOnClose>
      <participating-vehicles :id="maId" @close="closeDialog"></participating-vehicles>
    </BaseDialog>
  </div>
</template>

<script>
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { dictionary } from '@/utils/dictMixin';
import participatingVehicles from './participatingVehicles';
import { isEmpty } from '@/utils/utils';
import { getMaExecuteCondition, exportMaExecuteCondition,exporturl } from '@/api/repair/batchEventManagement/activityExecutionStatusQueryJingxsDuan';
import { arrayObjDeWeight } from '@/utils';

export default {
  name: 'index',
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '审核' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      visibleDetail: false,
      fetchApi: getMaExecuteCondition,
      dialogTitle: '',
      fetchParams: { xhrAbort: false },
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      maId: '',
      areaId: ''
    };
  },
  components: {
    participatingVehicles
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    async exportHandle() {
      const [err, params] = await this.$refs.topfilter.GET_FORM_DATA();
      await this.downHandleFile(exportMaExecuteCondition, params, '批量活动执行情况查询');
    },

    // 导出处理函数
    handleExportFun(row) {
      let labourAmount = isEmpty(row['labourAmount']) ? '0' : row['labourAmount'];
      let repairPartAmount = isEmpty(row['repairPartAmount']) ? '0' : row['repairPartAmount'];
      let addItemAmount = isEmpty(row['addItemAmount']) ? '0' : row['addItemAmount'];

      let total = parseFloat(labourAmount) + parseFloat(repairPartAmount) + parseFloat(addItemAmount);

      row.total = total;

      row.maStatus = this.createDictText(row.maStatus, 1014);

      row.dataSource = this.createDictText(row.dataSource, 1015);
    },

    /*经销商查询*/
    seachFun() {
      this.visible = true;
    },

    closeDialog() {
      this.visible = false;
    },

    async initSelect() {
      this.getAllUsers(this.columns, ['maBy'], 'table');
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  详情
                </el-button>
              </div>
            );
          }
        },
        {
          title: '活动编号',
          dataIndex: 'maNo',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '活动名称',
          dataIndex: 'maTitle',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开始日期',
          dataIndex: 'bDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '结束日期',
          dataIndex: 'eDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '活动状态',
          dataIndex: 'maStatus',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1014),
          render: props => {
            return <span>{this.createDictText(props.row.maStatus, 1014)}</span>;
          }
        },
        {
          title: '发布日期',
          dataIndex: 'maDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '工时费',
          dataIndex: 'labourAmount',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '维修材料费',
          dataIndex: 'repairPartAmount',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '附加项目费',
        //   dataIndex: 'addItemAmount',
        //   width: 150,
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '活动总金额',
          dataIndex: 'total',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            let row = props.row;

            let labourAmount = isEmpty(row['labourAmount']) ? '0' : row['labourAmount'];
            let repairPartAmount = isEmpty(row['repairPartAmount']) ? '0' : row['repairPartAmount'];
            // let addItemAmount = isEmpty(row['addItemAmount']) ? '0' : row['addItemAmount'];
            // let total = parseFloat(labourAmount) + parseFloat(repairPartAmount) + parseFloat(addItemAmount);
            let total = parseFloat(labourAmount) + parseFloat(repairPartAmount);

            return <span>{total}</span>;
          }
        },
        {
          title: '完成车辆数',
          dataIndex: 'count',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSource',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1015),
          render: props => {
            return <span>{this.createDictText(props.row.dataSource, 1015)}</span>;
          }
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '活动编号',
          fieldName: 'maNo',
          placeholder: '活动编号...'
        },
        {
          type: 'INPUT',
          label: '活动名称',
          fieldName: 'maTitle',
          placeholder: '活动名称...'
        },
        {
          type: 'RANGE_DATE',
          label: '活动日期',
          dateType: 'daterange',
          style: { minWidth: '200px' },
          fieldName: 'bDate|eDate'
        }
      ];
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    detailHandle(row) {
      this.handleType = 'show';
      this.dialogTitle = `${this.conf[this.handleType]}参与车辆明细`;
      this.maId = row['maId'];
      this.visible = true;
    },

    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    }
  }
};
</script>

<style lang="less" scoped></style>
